<template>
  <div class="side">
    <el-radio-group v-model="vipRadio" style="margin-bottom:15px">
      <el-radio-button label="会员设置"></el-radio-button>
      <el-radio-button label="会员等级设置"></el-radio-button>
    </el-radio-group>
    <div v-if="vipRadio==='会员设置'">
      <div class="register">
        <el-row>
          <el-button type="primary" @click="createUserSet" class="dakai">提交</el-button>
        </el-row>
      </div>
    </div>
    <div v-if="vipRadio==='会员等级设置'">
      <div class="regist">
        <el-button type="primary" @click="addCLick()" class="add">新增</el-button>
      </div>
    </div>
    <!-- 新增会员等级弹框 -->
    <el-dialog :visible.sync="addVip" :title="UserLevel.id ? '编辑' : '创建'" width="38%">
      <div style="width:100%;float:left;height:380px;">
        <el-form :model="UserLevel" label-position="right" label-width="120px" ref="dataForm">
          <el-form-item label="会员等级名称">
            <el-input v-model="UserLevel.name" style="width:80%"/>
          </el-form-item>
          <el-form-item label="购买价格">
            包月
            <el-input v-model="UserLevel.monthPrice" style="width:50%;margin-bottom:5px;"/>
            <span>元</span>
            <br/>
            包年
            <el-input v-model="UserLevel.yearPrice" style="width:50%"/>
            <span>元</span>
          </el-form-item>
          <el-form-item label="折扣">
            <el-input v-model="UserLevel.discount" style="width:50%"/>
            <span>%(1-100)</span>
          </el-form-item>
          <el-form-item label="顺序">
            <el-input v-model="UserLevel.sortField" style="width:80%"/>
          </el-form-item>
          <el-form-item label="一句话描述">
            <el-input v-model="UserLevel.describe" style="width:80%"/>
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addVip = false">取 消</el-button>
        <el-button type="primary" @click="createData()">保 存</el-button>
      </div>
    </el-dialog>
    <!-- 新增会员等级弹框 -->
    <el-form :model="UserSet" label-position="right" label-width="183px" style="margin-top:20px;" ref="userSetForm">
      <div v-if="vipRadio==='会员设置'">
        <el-form-item label="专区状态">
          <el-radio-group v-model="UserSet.status">
            <el-radio label="1">开启</el-radio>
            <el-radio label="0">关闭</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="小程序/微网校 会员功能">
          <el-radio-group v-model="UserSet.appStatus">
            <el-radio label="1">开启</el-radio>
            <el-radio label="0">关闭</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="购买方式">
          <el-radio-group v-model="UserSet.buyType">
            <el-radio label="1">按年月</el-radio>
            <el-radio label="2">按年</el-radio>
            <el-radio label="3">按月</el-radio>
          </el-radio-group>
          <!-- 按年月 -->
          <div style="margin-top:10px;" v-if="UserSet.buyType === 1">
            <el-row>
              <el-col :span="2"><p style="width:100px;height:10px;line-height:10px;color:#606266">默认购买月数</p></el-col>
              <el-col :span="2">
                <el-input v-model="UserSet.months" placeholder="请输入内容"></el-input>
              </el-col>
              <el-col :span="3"><p style="width:116px;height:10px;line-height:10px;color:#606266"> 个月 默认购买年数</p></el-col>
              <el-col :span="2">
                <el-input v-model="UserSet.years" placeholder="请输入内容"></el-input>
              </el-col>
              <el-col :span="2"><p style="width:75px;height:10px;line-height:10px;color:#606266"> 年</p></el-col>
            </el-row>
          </div>
          <!-- 按年 -->
          <div style="margin-top:10px;" v-if="UserSet.buyType === 2">
            <el-row>
              <el-col :span="2"><p style="width:75px;height:10px;line-height:10px;color:#606266">默认购买</p></el-col>
              <el-col :span="2">
                <el-input v-model="UserSet.years" placeholder="请输入内容"></el-input>
              </el-col>
              <el-col :span="2"><p style="width:75px;height:10px;line-height:10px;color:#606266"> 年</p></el-col>
            </el-row>
          </div>
          <!-- 按月 -->
          <div style="margin-top:10px;" v-if="UserSet.buyType === 3">
            <el-row>
              <el-col :span="2"><p style="width:75px;height:10px;line-height:10px;color:#606266">默认购买</p></el-col>
              <el-col :span="2">
                <el-input v-model="UserSet.months" placeholder="请输入内容"></el-input>
              </el-col>
              <el-col :span="2"><p style="width:75px;height:10px;line-height:10px;color:#606266"> 月</p></el-col>
            </el-row>
          </div>
        </el-form-item>
        <el-form-item label="会员到期提醒">
          <el-radio-group v-model="UserSet.remind">
            <el-radio label="1">开启</el-radio>
            <el-radio label="0">关闭</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="海报上传">
          <img v-if="UserSet.url" :src="UserSet.url" alt class="img" style="width:424px;height:124px;">
          <el-upload
            :on-change="handleLogoChange"
            :http-request="uploadLogoFile"
            :limit="1"
            action=""
            class="upload-demo"
            accept="image/jpg, image/png"
          >
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，建议图片尺寸为1140*300，且不超过500kb</div>
            <el-button slot="trigger" size="small" type="primary">点击上传</el-button>
          </el-upload>
        </el-form-item>
        <el-form-item label="背景颜色填充">
          <el-row>
            <el-col :span="2">
              <el-input v-model="UserSet.backgroundColor" placeholder="#d3293c"></el-input>
            </el-col>
          </el-row>
        </el-form-item>
      </div>
    </el-form>
    <div v-if="vipRadio==='会员等级设置'">
      <!-- 会员设置 -->
      <subTable :table-label="tableLabel" :table-data="tableData">
        <!-- 调用公告组件(EL-table) slot-button  插入需要按钮模板-->
        <el-table-column slot="button" label="操作" align="center">
          <template slot-scope="scope">
            <el-button @click="updateCLick(scope.row)" type="primary" icon="el-icon-edit" circle title='修改'></el-button>
            <el-button @click="deleteCLick(scope.row)" type="danger" icon="el-icon-delete" circle title='删除'></el-button>
          </template>
        </el-table-column>
      </subTable>
    </div>

    <!-- 删除 -->
    <el-dialog :visible.sync="handleDelete" width="25%">
      <div style="width:100%;float:left;height:42px;">确定要删除吗?</div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="deleteData(deleteRow)">确定</el-button>
        <el-button @click="handleDelete = false">取消</el-button>
      </div>
    </el-dialog>
    <!-- 删除 -->
  </div>
</template>

<script>
  import subTable from "@/components/Eltable";
  import {add, deleteUserLevel, list} from '@/api/system/user_vip';
  import {addUserSet, getUserSet} from '@/api/system/user_set';
  import {getTenantId} from '@/utils/auth';
  import {upLoadFile} from '@/api/upLoadFile'; // 数据接口

  export default {
    components: {
      subTable
    },
    data() {
      return {
        vipRadio: '会员设置',
        logofile: '',
        dialogData: {
          addUser: {
            title: ''
          },
          addMoreUser: {
            title: '',
            uploadUrl: '',
            fileList: []
          }
        },
        name: '1',
        addVip: false,
        UserSet: {
          status: '',
          appStatus: '',
          buyType: '',
          days: '',
          remind: '',
          url: '',
          years: '',
          months: '',
          backgroundColor: '',
        },
        UserLevel: {
          name: '',
          monthPrice: '',
          yearPrice: '',
          userNum: '',
          joinVip: '',
          describe: '',
          sortField: '',
        },
        tableLabel: [
          {
            label: "会员等级名称",
            prop: "name"
          },
          {
            label: "月费价格",
            prop: "monthPrice"
          },
          {
            label: "年费价格",
            prop: "yearPrice"
          },
          {
            label: "折扣",
            prop: "discount"
          },
          {
            label: "顺序",
            prop: "sortField"
          }
        ],
        tableData: [],
        isShow: true,
        isSet: true,
        // 列表编辑按钮
        updateCLick(row) {
          this.addVip = true;
          this.UserLevel = Object.assign({}, row)
        },
        handleDelete: false,//删除
        deleteRow: '',
        deleteCLick(row) {
          this.handleDelete = true;
          this.deleteRow = row;
        },
        addCLick() {
          this.addVip = true;
          this.UserLevel = {}
        }
      };
    },
    created() {
      this.fetchData();
      this.getUserSet();
    },
    methods: {
      fetchData() {
        list(getTenantId()).then(response => {
          this.tableData = response.data.items;
          this.total = response.data.items.length;
          this.listFilter = response.data.items;
        });
      },
      createData() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.UserLevel.tenantId = getTenantId();
            add(this.UserLevel).then(() => {
              this.$notify({
                title: '成功',
                message: '操作成功',
                type: 'success',
                duration: 2000
              });
              this.fetchData();
              this.addVip = false;
            })
          }
        })
      },
      getUserSet() {
        getUserSet(getTenantId()).then(response => {
          this.UserSet = response.data
        }).catch(err => {
          console.log(err)
        })
      },
      createUserSet() {
        this.$refs['userSetForm'].validate((valid) => {
          if (valid) {
            this.UserSet.tenantId = getTenantId();
            addUserSet(this.UserSet).then(() => {
              this.$notify({
                title: '成功',
                message: '创建成功',
                type: 'success',
                duration: 2000
              });
              this.getUserSet();
            })
          }
        })
      },
      handleRemove(file, fileList) {
        console.log(file, fileList);
      },
      handleLogoChange(file) {
        this.logofile = file.raw
      },
      uploadLogoFile() {
        const fileForm = new FormData()
        fileForm.append('name', this.logofile.name)
        fileForm.append('uid', this.logofile.uid)
        fileForm.append('file', this.logofile)
        upLoadFile(fileForm).then(response => {
          this.UserSet.url = response.data.url
        })
      },
      updateData() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            add(this.UserLevel).then(() => {
              this.$notify({
                title: '成功',
                message: '更新成功',
                type: 'success',
                duration: 2000
              });
              this.fetchData();
              this.$refs['dataForm'].resetFields();
            })
          }
        })
      },
      deleteData(row) {
        this.handleDelete = false;
        deleteUserLevel(row.id).then(() => {
          this.$notify({
            title: '成功',
            message: '删除成功',
            type: 'success',
            duration: 2000
          });
          this.fetchData();
        })
      }
    }
  };
</script>

<style>
  .register {
    width: 100%;
    height: 44px;
    line-height: 44px;
    padding-left: 10px;
    background-color: #e7e9ed;
    padding-top: 15px;
    padding-right: 34px;
  }

  .regist {
    width: 100%;
    height: 44px;
    line-height: 44px;
    padding-top: 3px;
    background-color: #e7e9ed;
  }

  .add {
    float: right;
    position: absolute;
    right: 45px;
  }

  .foot {
    margin-top: 20px;
    padding: 15px 15px;
    border-radius: 5px;
    background-color: #d9edf7;
    color: #326f8f;
    border-left: 5px solid #50bfff;
  }

  .foot > ul {
    text-decoration: none;
    margin: 0;
    padding: 0;
    list-style: none;
  }

  .foot > ul > li {
    margin-top: 5px;
  }
</style>
